
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Javascript教程 - XMLHttpRequest 对象 - 小白AI.易名</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="Javascript教程"><meta name="keywords" content="小白AI.易名,xiaobaiai.net,Javascript,入门,教程,博客">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小白AI.易名">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-page-footer-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="same-origin.html" />
    
    
    <link rel="prev" href="cookie.html" />
    

    
        <link rel="shortcut icon" href='../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="bookmark" href='../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../../assets/images/apple-touch-icon.png'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../../assets/images/apple-touch-icon-120x120.png">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../../assets/images/apple-touch-icon-180x180.png">
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basic/">
            
                <a href="../basic/">
            
                    
                    入门篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../basic/introduction.html">
            
                <a href="../basic/introduction.html">
            
                    
                    导论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../basic/history.html">
            
                <a href="../basic/history.html">
            
                    
                    历史
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../basic/grammar.html">
            
                <a href="../basic/grammar.html">
            
                    
                    基本语法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../types/">
            
                <a href="../types/">
            
                    
                    数据类型
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../types/general.html">
            
                <a href="../types/general.html">
            
                    
                    概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../types/null-undefined-boolean.html">
            
                <a href="../types/null-undefined-boolean.html">
            
                    
                    null,undefined和布尔值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../types/number.html">
            
                <a href="../types/number.html">
            
                    
                    数值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../types/string.html">
            
                <a href="../types/string.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../types/object.html">
            
                <a href="../types/object.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../types/function.html">
            
                <a href="../types/function.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../types/array.html">
            
                <a href="../types/array.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../operators/">
            
                <a href="../operators/">
            
                    
                    运算符
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../operators/arithmetic.html">
            
                <a href="../operators/arithmetic.html">
            
                    
                    算术运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../operators/comparison.html">
            
                <a href="../operators/comparison.html">
            
                    
                    比较运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../operators/boolean.html">
            
                <a href="../operators/boolean.html">
            
                    
                    布尔运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../operators/bit.html">
            
                <a href="../operators/bit.html">
            
                    
                    二进制位运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../operators/priority.html">
            
                <a href="../operators/priority.html">
            
                    
                    其他运算符，运算顺序
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../features/">
            
                <a href="../features/">
            
                    
                    语法专题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../features/conversion.html">
            
                <a href="../features/conversion.html">
            
                    
                    数据类型的转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../features/error.html">
            
                <a href="../features/error.html">
            
                    
                    错误处理机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../features/style.html">
            
                <a href="../features/style.html">
            
                    
                    编程风格
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../features/console.html">
            
                <a href="../features/console.html">
            
                    
                    console 对象与控制台
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../stdlib/">
            
                <a href="../stdlib/">
            
                    
                    标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../stdlib/object.html">
            
                <a href="../stdlib/object.html">
            
                    
                    Object 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../stdlib/attributes.html">
            
                <a href="../stdlib/attributes.html">
            
                    
                    属性描述对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../stdlib/array.html">
            
                <a href="../stdlib/array.html">
            
                    
                    Array 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../stdlib/wrapper.html">
            
                <a href="../stdlib/wrapper.html">
            
                    
                    包装对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../stdlib/boolean.html">
            
                <a href="../stdlib/boolean.html">
            
                    
                    Boolean 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../stdlib/number.html">
            
                <a href="../stdlib/number.html">
            
                    
                    Number 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../stdlib/string.html">
            
                <a href="../stdlib/string.html">
            
                    
                    String 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../stdlib/math.html">
            
                <a href="../stdlib/math.html">
            
                    
                    Math 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../stdlib/date.html">
            
                <a href="../stdlib/date.html">
            
                    
                    Date 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../stdlib/regexp.html">
            
                <a href="../stdlib/regexp.html">
            
                    
                    RegExp 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../stdlib/json.html">
            
                <a href="../stdlib/json.html">
            
                    
                    JSON 对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../oop/">
            
                <a href="../oop/">
            
                    
                    面向对象编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../oop/new.html">
            
                <a href="../oop/new.html">
            
                    
                    实例对象与 New 命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../oop/this.html">
            
                <a href="../oop/this.html">
            
                    
                    this 关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../oop/prototype.html">
            
                <a href="../oop/prototype.html">
            
                    
                    对象的继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../oop/object.html">
            
                <a href="../oop/object.html">
            
                    
                    Object 对象的相关方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../oop/strict.html">
            
                <a href="../oop/strict.html">
            
                    
                    严格模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../async/">
            
                <a href="../async/">
            
                    
                    异步操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../async/general.html">
            
                <a href="../async/general.html">
            
                    
                    概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../async/timer.html">
            
                <a href="../async/timer.html">
            
                    
                    定时器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../async/promise.html">
            
                <a href="../async/promise.html">
            
                    
                    Promise 对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../dom/">
            
                <a href="../dom/">
            
                    
                    DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../dom/general.html">
            
                <a href="../dom/general.html">
            
                    
                    概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../dom/node.html">
            
                <a href="../dom/node.html">
            
                    
                    Node 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../dom/nodelist.html">
            
                <a href="../dom/nodelist.html">
            
                    
                    NodeList 接口，HTMLCollection 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../dom/parentnode.html">
            
                <a href="../dom/parentnode.html">
            
                    
                    ParentNode 接口，ChildNode 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../dom/document.html">
            
                <a href="../dom/document.html">
            
                    
                    Document 节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../dom/element.html">
            
                <a href="../dom/element.html">
            
                    
                    Element 节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" data-path="../dom/attributes.html">
            
                <a href="../dom/attributes.html">
            
                    
                    属性的操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.8" data-path="../dom/text.html">
            
                <a href="../dom/text.html">
            
                    
                    Text 节点和 DocumentFragment 节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.9" data-path="../dom/css.html">
            
                <a href="../dom/css.html">
            
                    
                    CSS 操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.10" data-path="../dom/mutationobserver.html">
            
                <a href="../dom/mutationobserver.html">
            
                    
                    Mutation Observer API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../events/">
            
                <a href="../events/">
            
                    
                    事件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../events/eventtarget.html">
            
                <a href="../events/eventtarget.html">
            
                    
                    EventTarget 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../events/model.html">
            
                <a href="../events/model.html">
            
                    
                    事件模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../events/event.html">
            
                <a href="../events/event.html">
            
                    
                    Event 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../events/mouse.html">
            
                <a href="../events/mouse.html">
            
                    
                    鼠标事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../events/keyboard.html">
            
                <a href="../events/keyboard.html">
            
                    
                    键盘事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../events/progress.html">
            
                <a href="../events/progress.html">
            
                    
                    进度事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../events/form.html">
            
                <a href="../events/form.html">
            
                    
                    表单事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../events/touch.html">
            
                <a href="../events/touch.html">
            
                    
                    触摸事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../events/drag.html">
            
                <a href="../events/drag.html">
            
                    
                    拖拉事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.10" data-path="../events/common.html">
            
                <a href="../events/common.html">
            
                    
                    其他常见事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.11" data-path="../events/globaleventhandlers.html">
            
                <a href="../events/globaleventhandlers.html">
            
                    
                    GlobalEventHandlers 接口
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="./">
            
                <a href="./">
            
                    
                    浏览器模型
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="engine.html">
            
                <a href="engine.html">
            
                    
                    浏览器模型概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="window.html">
            
                <a href="window.html">
            
                    
                    window 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="navigator.html">
            
                <a href="navigator.html">
            
                    
                    Navigatror 对象，Screen 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="cookie.html">
            
                <a href="cookie.html">
            
                    
                    Cookie
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11.5" data-path="xmlhttprequest.html">
            
                <a href="xmlhttprequest.html">
            
                    
                    XMLHttpRequest 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.6" data-path="same-origin.html">
            
                <a href="same-origin.html">
            
                    
                    同源限制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.7" data-path="cors.html">
            
                <a href="cors.html">
            
                    
                    CORS 通信
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.8" data-path="storage.html">
            
                <a href="storage.html">
            
                    
                    Storage 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.9" data-path="history.html">
            
                <a href="history.html">
            
                    
                    History 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.10" data-path="location.html">
            
                <a href="location.html">
            
                    
                    Location对象，URL对象，URLSearchParams 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.11" data-path="arraybuffer.html">
            
                <a href="arraybuffer.html">
            
                    
                    ArrayBuffer 对象，Blob对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.12" data-path="file.html">
            
                <a href="file.html">
            
                    
                    File 对象，FileList 对象，FileReader 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.13" data-path="form.html">
            
                <a href="form.html">
            
                    
                    表单，FormData 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.14" data-path="indexeddb.html">
            
                <a href="indexeddb.html">
            
                    
                    IndexedDB API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.15" data-path="webworker.html">
            
                <a href="webworker.html">
            
                    
                    web worker
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../elements/">
            
                <a href="../elements/">
            
                    
                    附录:网页元素接口
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../elements/a.html">
            
                <a href="../elements/a.html">
            
                    
                    a超链接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../elements/image.html">
            
                <a href="../elements/image.html">
            
                    
                    image图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../elements/form.html">
            
                <a href="../elements/form.html">
            
                    
                    form表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../elements/input.html">
            
                <a href="../elements/input.html">
            
                    
                    input文本框
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="../elements/button.html">
            
                <a href="../elements/button.html">
            
                    
                    button按钮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.6" data-path="../elements/option.html">
            
                <a href="../elements/option.html">
            
                    
                    option选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.7" data-path="../elements/video.html">
            
                <a href="../elements/video.html">
            
                    
                    video,audio音视频
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="_blank" rel="nofollow" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >XMLHttpRequest 对象</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="xmlhttprequest-&#x5BF9;&#x8C61;">XMLHttpRequest &#x5BF9;&#x8C61;</h1>
<h2 id="&#x7B80;&#x4ECB;">&#x7B80;&#x4ECB;</h2>
<p>&#x6D4F;&#x89C8;&#x5668;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x4E4B;&#x95F4;&#xFF0C;&#x91C7;&#x7528; HTTP &#x534F;&#x8BAE;&#x901A;&#x4FE1;&#x3002;&#x7528;&#x6237;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x5730;&#x5740;&#x680F;&#x952E;&#x5165;&#x4E00;&#x4E2A;&#x7F51;&#x5740;&#xFF0C;&#x6216;&#x8005;&#x901A;&#x8FC7;&#x7F51;&#x9875;&#x8868;&#x5355;&#x5411;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#x5185;&#x5BB9;&#xFF0C;&#x8FD9;&#x65F6;&#x6D4F;&#x89C8;&#x5668;&#x5C31;&#x4F1A;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x51FA; HTTP &#x8BF7;&#x6C42;&#x3002;</p>
<p>1999&#x5E74;&#xFF0C;&#x5FAE;&#x8F6F;&#x516C;&#x53F8;&#x53D1;&#x5E03; IE &#x6D4F;&#x89C8;&#x5668;5.0&#x7248;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x5F15;&#x5165;&#x65B0;&#x529F;&#x80FD;&#xFF1A;&#x5141;&#x8BB8; JavaScript &#x811A;&#x672C;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x8D77; HTTP &#x8BF7;&#x6C42;&#x3002;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x5F53;&#x65F6;&#x5E76;&#x6CA1;&#x6709;&#x5F15;&#x8D77;&#x6CE8;&#x610F;&#xFF0C;&#x76F4;&#x5230;2004&#x5E74; Gmail &#x53D1;&#x5E03;&#x548C;2005&#x5E74; Google Map &#x53D1;&#x5E03;&#xFF0C;&#x624D;&#x5F15;&#x8D77;&#x5E7F;&#x6CDB;&#x91CD;&#x89C6;&#x3002;2005&#x5E74;2&#x6708;&#xFF0C;AJAX &#x8FD9;&#x4E2A;&#x8BCD;&#x7B2C;&#x4E00;&#x6B21;&#x6B63;&#x5F0F;&#x63D0;&#x51FA;&#xFF0C;&#x5B83;&#x662F; Asynchronous JavaScript and XML &#x7684;&#x7F29;&#x5199;&#xFF0C;&#x6307;&#x7684;&#x662F;&#x901A;&#x8FC7; JavaScript &#x7684;&#x5F02;&#x6B65;&#x901A;&#x4FE1;&#xFF0C;&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x83B7;&#x53D6; XML &#x6587;&#x6863;&#x4ECE;&#x4E2D;&#x63D0;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x518D;&#x66F4;&#x65B0;&#x5F53;&#x524D;&#x7F51;&#x9875;&#x7684;&#x5BF9;&#x5E94;&#x90E8;&#x5206;&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x5237;&#x65B0;&#x6574;&#x4E2A;&#x7F51;&#x9875;&#x3002;&#x540E;&#x6765;&#xFF0C;AJAX &#x8FD9;&#x4E2A;&#x8BCD;&#x5C31;&#x6210;&#x4E3A; JavaScript &#x811A;&#x672C;&#x53D1;&#x8D77; HTTP &#x901A;&#x4FE1;&#x7684;&#x4EE3;&#x540D;&#x8BCD;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x53EA;&#x8981;&#x7528;&#x811A;&#x672C;&#x53D1;&#x8D77;&#x901A;&#x4FE1;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x53EB;&#x505A; AJAX &#x901A;&#x4FE1;&#x3002;W3C &#x4E5F;&#x5728;2006&#x5E74;&#x53D1;&#x5E03;&#x4E86;&#x5B83;&#x7684;&#x56FD;&#x9645;&#x6807;&#x51C6;&#x3002;</p>
<p>&#x5177;&#x4F53;&#x6765;&#x8BF4;&#xFF0C;AJAX &#x5305;&#x62EC;&#x4EE5;&#x4E0B;&#x51E0;&#x4E2A;&#x6B65;&#x9AA4;&#x3002;</p>
<ol>
<li>&#x521B;&#x5EFA; XMLHttpRequest &#x5B9E;&#x4F8B;</li>
<li>&#x53D1;&#x51FA; HTTP &#x8BF7;&#x6C42;</li>
<li>&#x63A5;&#x6536;&#x670D;&#x52A1;&#x5668;&#x4F20;&#x56DE;&#x7684;&#x6570;&#x636E;</li>
<li>&#x66F4;&#x65B0;&#x7F51;&#x9875;&#x6570;&#x636E;</li>
</ol>
<p>&#x6982;&#x62EC;&#x8D77;&#x6765;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x53E5;&#x8BDD;&#xFF0C;AJAX &#x901A;&#x8FC7;&#x539F;&#x751F;&#x7684;<code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x53D1;&#x51FA; HTTP &#x8BF7;&#x6C42;&#xFF0C;&#x5F97;&#x5230;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x540E;&#xFF0C;&#x518D;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x662F; JSON &#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#xFF0C;XML &#x683C;&#x5F0F;&#x5DF2;&#x7ECF;&#x8FC7;&#x65F6;&#x4E86;&#xFF0C;&#x4F46;&#x662F; AJAX &#x8FD9;&#x4E2A;&#x540D;&#x5B57;&#x5DF2;&#x7ECF;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x901A;&#x7528;&#x540D;&#x8BCD;&#xFF0C;&#x5B57;&#x9762;&#x542B;&#x4E49;&#x5DF2;&#x7ECF;&#x6D88;&#x5931;&#x4E86;&#x3002;</p>
<p><code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x662F; AJAX &#x7684;&#x4E3B;&#x8981;&#x63A5;&#x53E3;&#xFF0C;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x4E4B;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x3002;&#x5C3D;&#x7BA1;&#x540D;&#x5B57;&#x91CC;&#x9762;&#x6709;<code>XML</code>&#x548C;<code>Http</code>&#xFF0C;&#x5B83;&#x5B9E;&#x9645;&#x4E0A;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x591A;&#x79CD;&#x534F;&#x8BAE;&#xFF08;&#x6BD4;&#x5982;<code>file</code>&#x6216;<code>ftp</code>&#xFF09;&#xFF0C;&#x53D1;&#x9001;&#x4EFB;&#x4F55;&#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#xFF08;&#x5305;&#x62EC;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x4E8C;&#x8FDB;&#x5236;&#xFF09;&#x3002;</p>
<p><code>XMLHttpRequest</code>&#x672C;&#x8EAB;&#x662F;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>new</code>&#x547D;&#x4EE4;&#x751F;&#x6210;&#x5B9E;&#x4F8B;&#x3002;&#x5B83;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
</code></pre>
<p>&#x4E00;&#x65E6;&#x65B0;&#x5EFA;&#x5B9E;&#x4F8B;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>open()</code>&#x65B9;&#x6CD5;&#x6307;&#x5B9A;&#x5EFA;&#x7ACB; HTTP &#x8FDE;&#x63A5;&#x7684;&#x4E00;&#x4E9B;&#x7EC6;&#x8282;&#x3002;</p>
<pre><code class="lang-javascript">xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;http://www.example.com/page.php&apos;</span>, <span class="hljs-literal">true</span>);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6307;&#x5B9A;&#x4F7F;&#x7528; GET &#x65B9;&#x6CD5;&#xFF0C;&#x8DDF;&#x6307;&#x5B9A;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7F51;&#x5740;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x3002;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;<code>true</code>&#xFF0C;&#x8868;&#x793A;&#x8BF7;&#x6C42;&#x662F;&#x5F02;&#x6B65;&#x7684;&#x3002;</p>
<p>&#x7136;&#x540E;&#xFF0C;&#x6307;&#x5B9A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x76D1;&#x542C;&#x901A;&#x4FE1;&#x72B6;&#x6001;&#xFF08;<code>readyState</code>&#x5C5E;&#x6027;&#xFF09;&#x7684;&#x53D8;&#x5316;&#x3002;</p>
<pre><code class="lang-javascript">xhr.onreadystatechange = handleStateChange;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleStateChange</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x4E00;&#x65E6;<code>XMLHttpRequest</code>&#x5B9E;&#x4F8B;&#x7684;&#x72B6;&#x6001;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x76D1;&#x542C;&#x51FD;&#x6570;<code>handleStateChange</code></p>
<p>&#x6700;&#x540E;&#x4F7F;&#x7528;<code>send()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5B9E;&#x9645;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x3002;</p>
<pre><code class="lang-javascript">xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>send()</code>&#x7684;&#x53C2;&#x6570;&#x4E3A;<code>null</code>&#xFF0C;&#x8868;&#x793A;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E0D;&#x5E26;&#x6709;&#x6570;&#x636E;&#x4F53;&#x3002;&#x5982;&#x679C;&#x53D1;&#x9001;&#x7684;&#x662F; POST &#x8BF7;&#x6C42;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x6570;&#x636E;&#x4F53;&#x3002;</p>
<p>&#x4E00;&#x65E6;&#x62FF;&#x5230;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#xFF0C;AJAX &#x4E0D;&#x4F1A;&#x5237;&#x65B0;&#x6574;&#x4E2A;&#x7F51;&#x9875;&#xFF0C;&#x800C;&#x662F;&#x53EA;&#x66F4;&#x65B0;&#x7F51;&#x9875;&#x91CC;&#x9762;&#x7684;&#x76F8;&#x5173;&#x90E8;&#x5206;&#xFF0C;&#x4ECE;&#x800C;&#x4E0D;&#x6253;&#x65AD;&#x7528;&#x6237;&#x6B63;&#x5728;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;AJAX &#x53EA;&#x80FD;&#x5411;&#x540C;&#x6E90;&#x7F51;&#x5740;&#xFF08;&#x534F;&#x8BAE;&#x3001;&#x57DF;&#x540D;&#x3001;&#x7AEF;&#x53E3;&#x90FD;&#x76F8;&#x540C;&#xFF09;&#x53D1;&#x51FA; HTTP &#x8BF7;&#x6C42;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x51FA;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#xFF0C;&#x5C31;&#x4F1A;&#x62A5;&#x9519;&#xFF08;&#x8BE6;&#x89C1;&#x300A;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x300B;&#x548C;&#x300A;CORS &#x901A;&#x4FE1;&#x300B;&#x4E24;&#x7AE0;&#xFF09;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;<code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x7B80;&#x5355;&#x7528;&#x6CD5;&#x7684;&#x5B8C;&#x6574;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();

xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-comment">// &#x901A;&#x4FE1;&#x6210;&#x529F;&#x65F6;&#xFF0C;&#x72B6;&#x6001;&#x503C;&#x4E3A;4</span>
  <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>){
    <span class="hljs-keyword">if</span> (xhr.status === <span class="hljs-number">200</span>){
      <span class="hljs-built_in">console</span>.log(xhr.responseText);
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">console</span>.error(xhr.statusText);
    }
  }
};

xhr.onerror = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
  <span class="hljs-built_in">console</span>.error(xhr.statusText);
};

xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;/endpoint&apos;</span>, <span class="hljs-literal">true</span>);
xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<h2 id="xmlhttprequest-&#x7684;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;">XMLHttpRequest &#x7684;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;</h2>
<h3 id="xmlhttprequestreadystate">XMLHttpRequest.readyState</h3>
<p><code>XMLHttpRequest.readyState</code>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x7684;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x3002;&#x8BE5;&#x5C5E;&#x6027;&#x53EA;&#x8BFB;&#x3002;&#x5B83;&#x53EF;&#x80FD;&#x8FD4;&#x56DE;&#x4EE5;&#x4E0B;&#x503C;&#x3002;</p>
<ul>
<li>0&#xFF0C;&#x8868;&#x793A; XMLHttpRequest &#x5B9E;&#x4F8B;&#x5DF2;&#x7ECF;&#x751F;&#x6210;&#xFF0C;&#x4F46;&#x662F;&#x5B9E;&#x4F8B;&#x7684;<code>open()</code>&#x65B9;&#x6CD5;&#x8FD8;&#x6CA1;&#x6709;&#x88AB;&#x8C03;&#x7528;&#x3002;</li>
<li>1&#xFF0C;&#x8868;&#x793A;<code>open()</code>&#x65B9;&#x6CD5;&#x5DF2;&#x7ECF;&#x8C03;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x5B9E;&#x4F8B;&#x7684;<code>send()</code>&#x65B9;&#x6CD5;&#x8FD8;&#x6CA1;&#x6709;&#x8C03;&#x7528;&#xFF0C;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B9E;&#x4F8B;&#x7684;<code>setRequestHeader()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x8BBE;&#x5B9A; HTTP &#x8BF7;&#x6C42;&#x7684;&#x5934;&#x4FE1;&#x606F;&#x3002;</li>
<li>2&#xFF0C;&#x8868;&#x793A;&#x5B9E;&#x4F8B;&#x7684;<code>send()</code>&#x65B9;&#x6CD5;&#x5DF2;&#x7ECF;&#x8C03;&#x7528;&#xFF0C;&#x5E76;&#x4E14;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x5934;&#x4FE1;&#x606F;&#x548C;&#x72B6;&#x6001;&#x7801;&#x5DF2;&#x7ECF;&#x6536;&#x5230;&#x3002;</li>
<li>3&#xFF0C;&#x8868;&#x793A;&#x6B63;&#x5728;&#x63A5;&#x6536;&#x670D;&#x52A1;&#x5668;&#x4F20;&#x6765;&#x7684;&#x6570;&#x636E;&#x4F53;&#xFF08;body &#x90E8;&#x5206;&#xFF09;&#x3002;&#x8FD9;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x5B9E;&#x4F8B;&#x7684;<code>responseType</code>&#x5C5E;&#x6027;&#x7B49;&#x4E8E;<code>text</code>&#x6216;&#x8005;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;<code>responseText</code>&#x5C5E;&#x6027;&#x5C31;&#x4F1A;&#x5305;&#x542B;&#x5DF2;&#x7ECF;&#x6536;&#x5230;&#x7684;&#x90E8;&#x5206;&#x4FE1;&#x606F;&#x3002;</li>
<li>4&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x5DF2;&#x7ECF;&#x5B8C;&#x5168;&#x63A5;&#x6536;&#xFF0C;&#x6216;&#x8005;&#x672C;&#x6B21;&#x63A5;&#x6536;&#x5DF2;&#x7ECF;&#x5931;&#x8D25;&#x3002;</li>
</ul>
<p>&#x901A;&#x4FE1;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x6BCF;&#x5F53;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x53D1;&#x751F;&#x72B6;&#x6001;&#x53D8;&#x5316;&#xFF0C;&#x5B83;&#x7684;<code>readyState</code>&#x5C5E;&#x6027;&#x7684;&#x503C;&#x5C31;&#x4F1A;&#x6539;&#x53D8;&#x3002;&#x8FD9;&#x4E2A;&#x503C;&#x6BCF;&#x4E00;&#x6B21;&#x53D8;&#x5316;&#xFF0C;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;<code>readyStateChange</code>&#x4E8B;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();

<span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>) {
  <span class="hljs-comment">// &#x8BF7;&#x6C42;&#x7ED3;&#x675F;&#xFF0C;&#x5904;&#x7406;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;</span>
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// &#x663E;&#x793A;&#x63D0;&#x793A;&#x201C;&#x52A0;&#x8F7D;&#x4E2D;&#x2026;&#x2026;&#x201D;</span>
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>xhr.readyState</code>&#x7B49;&#x4E8E;<code>4</code>&#x65F6;&#xFF0C;&#x8868;&#x660E;&#x811A;&#x672C;&#x53D1;&#x51FA;&#x7684; HTTP &#x8BF7;&#x6C42;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x3002;&#x5176;&#x4ED6;&#x60C5;&#x51B5;&#xFF0C;&#x90FD;&#x8868;&#x793A; HTTP &#x8BF7;&#x6C42;&#x8FD8;&#x5728;&#x8FDB;&#x884C;&#x4E2D;&#x3002;</p>
<h3 id="xmlhttprequestonreadystatechange">XMLHttpRequest.onreadystatechange</h3>
<p><code>XMLHttpRequest.onreadystatechange</code>&#x5C5E;&#x6027;&#x6307;&#x5411;&#x4E00;&#x4E2A;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x3002;<code>readystatechange</code>&#x4E8B;&#x4EF6;&#x53D1;&#x751F;&#x65F6;&#xFF08;&#x5B9E;&#x4F8B;&#x7684;<code>readyState</code>&#x5C5E;&#x6027;&#x53D8;&#x5316;&#xFF09;&#xFF0C;&#x5C31;&#x4F1A;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x5B9E;&#x4F8B;&#x7684;<code>abort()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x7EC8;&#x6B62; XMLHttpRequest &#x8BF7;&#x6C42;&#xFF0C;&#x4E5F;&#x4F1A;&#x9020;&#x6210;<code>readyState</code>&#x5C5E;&#x6027;&#x53D8;&#x5316;&#xFF0C;&#x5BFC;&#x81F4;&#x8C03;&#x7528;<code>XMLHttpRequest.onreadystatechange</code>&#x5C5E;&#x6027;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open( <span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;http://example.com&apos;</span> , <span class="hljs-literal">true</span> );
xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (xhr.readyState !== <span class="hljs-number">4</span> || xhr.status !== <span class="hljs-number">200</span>) {
    <span class="hljs-keyword">return</span>;
  }
  <span class="hljs-built_in">console</span>.log(xhr.responseText);
};
xhr.send();
</code></pre>
<h3 id="xmlhttprequestresponse">XMLHttpRequest.response</h3>
<p><code>XMLHttpRequest.response</code>&#x5C5E;&#x6027;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x4F53;&#xFF08;&#x5373; HTTP &#x56DE;&#x5E94;&#x7684; body &#x90E8;&#x5206;&#xFF09;&#x3002;&#x5B83;&#x53EF;&#x80FD;&#x662F;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x6BD4;&#x5982;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5BF9;&#x8C61;&#x3001;&#x4E8C;&#x8FDB;&#x5236;&#x5BF9;&#x8C61;&#x7B49;&#x7B49;&#xFF0C;&#x5177;&#x4F53;&#x7684;&#x7C7B;&#x578B;&#x7531;<code>XMLHttpRequest.responseType</code>&#x5C5E;&#x6027;&#x51B3;&#x5B9A;&#x3002;&#x8BE5;&#x5C5E;&#x6027;&#x53EA;&#x8BFB;&#x3002;</p>
<p>&#x5982;&#x679C;&#x672C;&#x6B21;&#x8BF7;&#x6C42;&#x6CA1;&#x6709;&#x6210;&#x529F;&#x6216;&#x8005;&#x6570;&#x636E;&#x4E0D;&#x5B8C;&#x6574;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x7B49;&#x4E8E;<code>null</code>&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5982;&#x679C;<code>responseType</code>&#x5C5E;&#x6027;&#x7B49;&#x4E8E;<code>text</code>&#x6216;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5728;&#x8BF7;&#x6C42;&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#x4E4B;&#x524D;&#xFF08;<code>readyState</code>&#x7B49;&#x4E8E;3&#x7684;&#x9636;&#x6BB5;&#xFF09;&#xFF0C;<code>response</code>&#x5C5E;&#x6027;&#x5305;&#x542B;&#x670D;&#x52A1;&#x5668;&#x5DF2;&#x7ECF;&#x8FD4;&#x56DE;&#x7684;&#x90E8;&#x5206;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();

xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>) {
    handler(xhr.response);
  }
}
</code></pre>
<h3 id="xmlhttprequestresponsetype">XMLHttpRequest.responseType</h3>
<p><code>XMLHttpRequest.responseType</code>&#x5C5E;&#x6027;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x662F;&#x53EF;&#x5199;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x8C03;&#x7528;<code>open()</code>&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#x3001;&#x8C03;&#x7528;<code>send()</code>&#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#xFF0C;&#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x503C;&#xFF0C;&#x544A;&#x8BC9;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x3002;&#x5982;&#x679C;<code>responseType</code>&#x8BBE;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5C31;&#x7B49;&#x540C;&#x4E8E;&#x9ED8;&#x8BA4;&#x503C;<code>text</code>&#x3002;</p>
<p><code>XMLHttpRequest.responseType</code>&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x7B49;&#x4E8E;&#x4EE5;&#x4E0B;&#x503C;&#x3002;</p>
<ul>
<li>&quot;&quot;&#xFF08;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#xFF09;&#xFF1A;&#x7B49;&#x540C;&#x4E8E;<code>text</code>&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x6587;&#x672C;&#x6570;&#x636E;&#x3002;</li>
<li>&quot;arraybuffer&quot;&#xFF1A;ArrayBuffer &#x5BF9;&#x8C61;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x7EC4;&#x3002;</li>
<li>&quot;blob&quot;&#xFF1A;Blob &#x5BF9;&#x8C61;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x4E8C;&#x8FDB;&#x5236;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&quot;document&quot;&#xFF1A;Document &#x5BF9;&#x8C61;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&quot;json&quot;&#xFF1A;JSON &#x5BF9;&#x8C61;&#x3002;</li>
<li>&quot;text&quot;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
</ul>
<p>&#x4E0A;&#x9762;&#x51E0;&#x79CD;&#x7C7B;&#x578B;&#x4E4B;&#x4E2D;&#xFF0C;<code>text</code>&#x7C7B;&#x578B;&#x9002;&#x5408;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#xFF0C;&#x800C;&#x4E14;&#x76F4;&#x63A5;&#x5904;&#x7406;&#x6587;&#x672C;&#x4E5F;&#x6BD4;&#x8F83;&#x65B9;&#x4FBF;&#x3002;<code>document</code>&#x7C7B;&#x578B;&#x9002;&#x5408;&#x8FD4;&#x56DE; HTML / XML &#x6587;&#x6863;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x5BF9;&#x4E8E;&#x90A3;&#x4E9B;&#x6253;&#x5F00; CORS &#x7684;&#x7F51;&#x7AD9;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528; Ajax &#x6293;&#x53D6;&#x7F51;&#x9875;&#xFF0C;&#x7136;&#x540E;&#x4E0D;&#x7528;&#x89E3;&#x6790; HTML &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x76F4;&#x63A5;&#x5BF9;&#x6293;&#x53D6;&#x56DE;&#x6765;&#x7684;&#x6570;&#x636E;&#x8FDB;&#x884C; DOM &#x64CD;&#x4F5C;&#x3002;<code>blob</code>&#x7C7B;&#x578B;&#x9002;&#x5408;&#x8BFB;&#x53D6;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#xFF0C;&#x6BD4;&#x5982;&#x56FE;&#x7247;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;/path/to/image.png&apos;</span>, <span class="hljs-literal">true</span>);
xhr.responseType = <span class="hljs-string">&apos;blob&apos;</span>;

xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.status === <span class="hljs-number">200</span>) {
    <span class="hljs-keyword">var</span> blob = <span class="hljs-keyword">new</span> Blob([xhr.response], {type: <span class="hljs-string">&apos;image/png&apos;</span>});
    <span class="hljs-comment">// &#x6216;&#x8005;</span>
    <span class="hljs-keyword">var</span> blob = xhr.response;
  }
};

xhr.send();
</code></pre>
<p>&#x5982;&#x679C;&#x5C06;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x8BBE;&#x4E3A;<code>ArrayBuffer</code>&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x6309;&#x7167;&#x6570;&#x7EC4;&#x7684;&#x65B9;&#x5F0F;&#x5904;&#x7406;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;/path/to/image.png&apos;</span>, <span class="hljs-literal">true</span>);
xhr.responseType = <span class="hljs-string">&apos;arraybuffer&apos;</span>;

xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-keyword">var</span> uInt8Array = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>(<span class="hljs-keyword">this</span>.response);
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, len = uInt8Array.length; i &lt; len; ++i) {
    <span class="hljs-comment">// var byte = uInt8Array[i];</span>
  }
};

xhr.send();
</code></pre>
<p>&#x5982;&#x679C;&#x5C06;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x8BBE;&#x4E3A;<code>json</code>&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;&#x5BF9;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x8C03;&#x7528;<code>JSON.parse()</code>&#x65B9;&#x6CD5;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x4ECE;<code>xhr.response</code>&#x5C5E;&#x6027;&#xFF08;&#x6CE8;&#x610F;&#xFF0C;&#x4E0D;&#x662F;<code>xhr.responseText</code>&#x5C5E;&#x6027;&#xFF09;&#x5F97;&#x5230;&#x7684;&#x4E0D;&#x662F;&#x6587;&#x672C;&#xFF0C;&#x800C;&#x662F;&#x4E00;&#x4E2A; JSON &#x5BF9;&#x8C61;&#x3002;</p>
<h3 id="xmlhttprequestresponsetext">XMLHttpRequest.responseText</h3>
<p><code>XMLHttpRequest.responseText</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x63A5;&#x6536;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x4E3A;&#x53EA;&#x8BFB;&#x3002;&#x53EA;&#x6709; HTTP &#x8BF7;&#x6C42;&#x5B8C;&#x6210;&#x63A5;&#x6536;&#x4EE5;&#x540E;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x624D;&#x4F1A;&#x5305;&#x542B;&#x5B8C;&#x6574;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;/server&apos;</span>, <span class="hljs-literal">true</span>);

xhr.responseType = <span class="hljs-string">&apos;text&apos;</span>;
xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span> &amp;&amp; xhr.status === <span class="hljs-number">200</span>) {
    <span class="hljs-built_in">console</span>.log(xhr.responseText);
  }
};

xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<h3 id="xmlhttprequestresponsexml">XMLHttpRequest.responseXML</h3>
<p><code>XMLHttpRequest.responseXML</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x63A5;&#x6536;&#x5230;&#x7684; HTML &#x6216; XML &#x6587;&#x6863;&#x5BF9;&#x8C61;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x4E3A;&#x53EA;&#x8BFB;&#x3002;&#x5982;&#x679C;&#x672C;&#x6B21;&#x8BF7;&#x6C42;&#x6CA1;&#x6709;&#x6210;&#x529F;&#xFF0C;&#x6216;&#x8005;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#x4E0D;&#x80FD;&#x88AB;&#x89E3;&#x6790;&#x4E3A; XML &#x6216; HTML&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x7B49;&#x4E8E;<code>null</code>&#x3002;</p>
<p>&#x8BE5;&#x5C5E;&#x6027;&#x751F;&#x6548;&#x7684;&#x524D;&#x63D0;&#x662F; HTTP &#x56DE;&#x5E94;&#x7684;<code>Content-Type</code>&#x5934;&#x4FE1;&#x606F;&#x7B49;&#x4E8E;<code>text/xml</code>&#x6216;<code>application/xml</code>&#x3002;&#x8FD9;&#x8981;&#x6C42;&#x5728;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x524D;&#xFF0C;<code>XMLHttpRequest.responseType</code>&#x5C5E;&#x6027;&#x8981;&#x8BBE;&#x4E3A;<code>document</code>&#x3002;&#x5982;&#x679C; HTTP &#x56DE;&#x5E94;&#x7684;<code>Content-Type</code>&#x5934;&#x4FE1;&#x606F;&#x4E0D;&#x7B49;&#x4E8E;<code>text/xml</code>&#x548C;<code>application/xml</code>&#xFF0C;&#x4F46;&#x662F;&#x60F3;&#x4ECE;<code>responseXML</code>&#x62FF;&#x5230;&#x6570;&#x636E;&#xFF08;&#x5373;&#x628A;&#x6570;&#x636E;&#x6309;&#x7167; DOM &#x683C;&#x5F0F;&#x89E3;&#x6790;&#xFF09;&#xFF0C;&#x90A3;&#x4E48;&#x9700;&#x8981;&#x624B;&#x52A8;&#x8C03;&#x7528;<code>XMLHttpRequest.overrideMimeType()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5F3A;&#x5236;&#x8FDB;&#x884C; XML &#x89E3;&#x6790;&#x3002;</p>
<p>&#x8BE5;&#x5C5E;&#x6027;&#x5F97;&#x5230;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x662F;&#x76F4;&#x63A5;&#x89E3;&#x6790;&#x540E;&#x7684;&#x6587;&#x6863; DOM &#x6811;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;/server&apos;</span>, <span class="hljs-literal">true</span>);

xhr.responseType = <span class="hljs-string">&apos;document&apos;</span>;
xhr.overrideMimeType(<span class="hljs-string">&apos;text/xml&apos;</span>);

xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span> &amp;&amp; xhr.status === <span class="hljs-number">200</span>) {
    <span class="hljs-built_in">console</span>.log(xhr.responseXML);
  }
};

xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<h3 id="xmlhttprequestresponseurl">XMLHttpRequest.responseURL</h3>
<p><code>XMLHttpRequest.responseURL</code>&#x5C5E;&#x6027;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7684;&#x7F51;&#x5740;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;http://example.com/test&apos;</span>, <span class="hljs-literal">true</span>);
xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// &#x8FD4;&#x56DE; http://example.com/test</span>
  <span class="hljs-built_in">console</span>.log(xhr.responseURL);
};
xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x503C;&#x4E0E;<code>open()</code>&#x65B9;&#x6CD5;&#x6307;&#x5B9A;&#x7684;&#x8BF7;&#x6C42;&#x7F51;&#x5740;&#x4E0D;&#x4E00;&#x5B9A;&#x76F8;&#x540C;&#x3002;&#x5982;&#x679C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x53D1;&#x751F;&#x8DF3;&#x8F6C;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x6700;&#x540E;&#x5B9E;&#x9645;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x7684;&#x7F51;&#x5740;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x5982;&#x679C;&#x539F;&#x59CB; URL &#x5305;&#x62EC;&#x951A;&#x70B9;&#xFF08;fragment&#xFF09;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x4F1A;&#x628A;&#x951A;&#x70B9;&#x5265;&#x79BB;&#x3002;</p>
<h3 id="xmlhttprequeststatus&#xFF0C;xmlhttprequeststatustext">XMLHttpRequest.status&#xFF0C;XMLHttpRequest.statusText</h3>
<p><code>XMLHttpRequest.status</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x56DE;&#x5E94;&#x7684; HTTP &#x72B6;&#x6001;&#x7801;&#x3002;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x901A;&#x4FE1;&#x6210;&#x529F;&#x7684;&#x8BDD;&#xFF0C;&#x8FD9;&#x4E2A;&#x72B6;&#x6001;&#x7801;&#x662F;200&#xFF1B;&#x5982;&#x679C;&#x670D;&#x52A1;&#x5668;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x9ED8;&#x8BA4;&#x662F;200&#x3002;&#x8BF7;&#x6C42;&#x53D1;&#x51FA;&#x4E4B;&#x524D;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x4E3A;<code>0</code>&#x3002;&#x8BE5;&#x5C5E;&#x6027;&#x53EA;&#x8BFB;&#x3002;</p>
<ul>
<li>200, OK&#xFF0C;&#x8BBF;&#x95EE;&#x6B63;&#x5E38;</li>
<li>301, Moved Permanently&#xFF0C;&#x6C38;&#x4E45;&#x79FB;&#x52A8;</li>
<li>302, Moved temporarily&#xFF0C;&#x6682;&#x65F6;&#x79FB;&#x52A8;</li>
<li>304, Not Modified&#xFF0C;&#x672A;&#x4FEE;&#x6539;</li>
<li>307, Temporary Redirect&#xFF0C;&#x6682;&#x65F6;&#x91CD;&#x5B9A;&#x5411;</li>
<li>401, Unauthorized&#xFF0C;&#x672A;&#x6388;&#x6743;</li>
<li>403, Forbidden&#xFF0C;&#x7981;&#x6B62;&#x8BBF;&#x95EE;</li>
<li>404, Not Found&#xFF0C;&#x672A;&#x53D1;&#x73B0;&#x6307;&#x5B9A;&#x7F51;&#x5740;</li>
<li>500, Internal Server Error&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x751F;&#x9519;&#x8BEF;</li>
</ul>
<p>&#x57FA;&#x672C;&#x4E0A;&#xFF0C;&#x53EA;&#x6709;2xx&#x548C;304&#x7684;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x662F;&#x6B63;&#x5E38;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>) {
  <span class="hljs-keyword">if</span> ( (xhr.status &gt;= <span class="hljs-number">200</span> &amp;&amp; xhr.status &lt; <span class="hljs-number">300</span>)
    || (xhr.status === <span class="hljs-number">304</span>) ) {
    <span class="hljs-comment">// &#x5904;&#x7406;&#x670D;&#x52A1;&#x5668;&#x7684;&#x8FD4;&#x56DE;&#x6570;&#x636E;</span>
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x51FA;&#x9519;</span>
  }
}
</code></pre>
<p><code>XMLHttpRequest.statusText</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x7684;&#x72B6;&#x6001;&#x63D0;&#x793A;&#x3002;&#x4E0D;&#x540C;&#x4E8E;<code>status</code>&#x5C5E;&#x6027;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x5305;&#x542B;&#x6574;&#x4E2A;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#xFF0C;&#x6BD4;&#x5982;&#x201C;OK&#x201D;&#x548C;&#x201C;Not Found&#x201D;&#x3002;&#x5728;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x4E4B;&#x524D;&#xFF08;&#x5373;&#x8C03;&#x7528;<code>open()</code>&#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#xFF09;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x503C;&#x662F;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#xFF1B;&#x5982;&#x679C;&#x670D;&#x52A1;&#x5668;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x63D0;&#x793A;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x503C;&#x9ED8;&#x8BA4;&#x4E3A;&#x201C;OK&#x201D;&#x3002;&#x8BE5;&#x5C5E;&#x6027;&#x4E3A;&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x3002;</p>
<h3 id="xmlhttprequesttimeout&#xFF0C;xmlhttprequesteventtargetontimeout">XMLHttpRequest.timeout&#xFF0C;XMLHttpRequestEventTarget.ontimeout</h3>
<p><code>XMLHttpRequest.timeout</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x591A;&#x5C11;&#x6BEB;&#x79D2;&#x540E;&#xFF0C;&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x4ECD;&#x7136;&#x6CA1;&#x6709;&#x5F97;&#x5230;&#x7ED3;&#x679C;&#xFF0C;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;&#x7EC8;&#x6B62;&#x3002;&#x5982;&#x679C;&#x8BE5;&#x5C5E;&#x6027;&#x7B49;&#x4E8E;0&#xFF0C;&#x5C31;&#x8868;&#x793A;&#x6CA1;&#x6709;&#x65F6;&#x95F4;&#x9650;&#x5236;&#x3002;</p>
<p><code>XMLHttpRequestEventTarget.ontimeout</code>&#x5C5E;&#x6027;&#x7528;&#x4E8E;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x76D1;&#x542C;&#x51FD;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x751F; timeout &#x4E8B;&#x4EF6;&#xFF0C;&#x5C31;&#x4F1A;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
<span class="hljs-keyword">var</span> url = <span class="hljs-string">&apos;/server&apos;</span>;

xhr.ontimeout = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.error(<span class="hljs-string">&apos;The request for &apos;</span> + url + <span class="hljs-string">&apos; timed out.&apos;</span>);
};

xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>) {
    <span class="hljs-keyword">if</span> (xhr.status === <span class="hljs-number">200</span>) {
      <span class="hljs-comment">// &#x5904;&#x7406;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;</span>
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">console</span>.error(xhr.statusText);
    }
  }
};

xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, url, <span class="hljs-literal">true</span>);
<span class="hljs-comment">// &#x6307;&#x5B9A; 10 &#x79D2;&#x949F;&#x8D85;&#x65F6;</span>
xhr.timeout = <span class="hljs-number">10</span> * <span class="hljs-number">1000</span>;
xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<h3 id="&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x5C5E;&#x6027;">&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x5C5E;&#x6027;</h3>
<p>XMLHttpRequest &#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5BF9;&#x4EE5;&#x4E0B;&#x4E8B;&#x4EF6;&#x6307;&#x5B9A;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x3002;</p>
<ul>
<li>XMLHttpRequest.onloadstart&#xFF1A;loadstart &#x4E8B;&#x4EF6;&#xFF08;HTTP &#x8BF7;&#x6C42;&#x53D1;&#x51FA;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
<li>XMLHttpRequest.onprogress&#xFF1A;progress&#x4E8B;&#x4EF6;&#xFF08;&#x6B63;&#x5728;&#x53D1;&#x9001;&#x548C;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
<li>XMLHttpRequest.onabort&#xFF1A;abort &#x4E8B;&#x4EF6;&#xFF08;&#x8BF7;&#x6C42;&#x4E2D;&#x6B62;&#xFF0C;&#x6BD4;&#x5982;&#x7528;&#x6237;&#x8C03;&#x7528;&#x4E86;<code>abort()</code>&#x65B9;&#x6CD5;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
<li>XMLHttpRequest.onerror&#xFF1A;error &#x4E8B;&#x4EF6;&#xFF08;&#x8BF7;&#x6C42;&#x5931;&#x8D25;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
<li>XMLHttpRequest.onload&#xFF1A;load &#x4E8B;&#x4EF6;&#xFF08;&#x8BF7;&#x6C42;&#x6210;&#x529F;&#x5B8C;&#x6210;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
<li>XMLHttpRequest.ontimeout&#xFF1A;timeout &#x4E8B;&#x4EF6;&#xFF08;&#x7528;&#x6237;&#x6307;&#x5B9A;&#x7684;&#x65F6;&#x9650;&#x8D85;&#x8FC7;&#x4E86;&#xFF0C;&#x8BF7;&#x6C42;&#x8FD8;&#x672A;&#x5B8C;&#x6210;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
<li>XMLHttpRequest.onloadend&#xFF1A;loadend &#x4E8B;&#x4EF6;&#xFF08;&#x8BF7;&#x6C42;&#x5B8C;&#x6210;&#xFF0C;&#x4E0D;&#x7BA1;&#x6210;&#x529F;&#x6216;&#x5931;&#x8D25;&#xFF09;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
 <span class="hljs-keyword">var</span> responseText = xhr.responseText;
 <span class="hljs-built_in">console</span>.log(responseText);
 <span class="hljs-comment">// process the response.</span>
};

xhr.onabort = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;The request was aborted&apos;</span>);
};

xhr.onprogress = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
  <span class="hljs-built_in">console</span>.log(event.loaded);
  <span class="hljs-built_in">console</span>.log(event.total);
};

xhr.onerror = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;There was an error!&apos;</span>);
};
</code></pre>
<p><code>progress</code>&#x4E8B;&#x4EF6;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x6709;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;&#x53C2;&#x6570;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x6709;&#x4E09;&#x4E2A;&#x5C5E;&#x6027;&#xFF1A;<code>loaded</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x5DF2;&#x7ECF;&#x4F20;&#x8F93;&#x7684;&#x6570;&#x636E;&#x91CF;&#xFF0C;<code>total</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x603B;&#x7684;&#x6570;&#x636E;&#x91CF;&#xFF0C;<code>lengthComputable</code>&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x52A0;&#x8F7D;&#x7684;&#x8FDB;&#x5EA6;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x8BA1;&#x7B97;&#x3002;&#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x91CC;&#x9762;&#xFF0C;&#x53EA;&#x6709;<code>progress</code>&#x4E8B;&#x4EF6;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x6709;&#x53C2;&#x6570;&#xFF0C;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x90FD;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x751F;&#x7F51;&#x7EDC;&#x9519;&#x8BEF;&#xFF08;&#x6BD4;&#x5982;&#x670D;&#x52A1;&#x5668;&#x65E0;&#x6CD5;&#x8FDE;&#x901A;&#xFF09;&#xFF0C;<code>onerror</code>&#x4E8B;&#x4EF6;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x62A5;&#x9519;&#x4FE1;&#x606F;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x53EF;&#x80FD;&#x6CA1;&#x6709;&#x9519;&#x8BEF;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x6837;&#x53EA;&#x80FD;&#x663E;&#x793A;&#x62A5;&#x9519;&#x7684;&#x63D0;&#x793A;&#x3002;</p>
<h3 id="xmlhttprequestwithcredentials">XMLHttpRequest.withCredentials</h3>
<p><code>XMLHttpRequest.withCredentials</code>&#x5C5E;&#x6027;&#x662F;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;&#x7528;&#x6237;&#x4FE1;&#x606F;&#xFF08;&#x6BD4;&#x5982; Cookie &#x548C;&#x8BA4;&#x8BC1;&#x7684; HTTP &#x5934;&#x4FE1;&#x606F;&#xFF09;&#x662F;&#x5426;&#x4F1A;&#x5305;&#x542B;&#x5728;&#x8BF7;&#x6C42;&#x4E4B;&#x4E2D;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;<code>false</code>&#xFF0C;&#x5373;&#x5411;<code>example.com</code>&#x53D1;&#x51FA;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;&#x4E0D;&#x4F1A;&#x53D1;&#x9001;<code>example.com</code>&#x8BBE;&#x7F6E;&#x5728;&#x672C;&#x673A;&#x4E0A;&#x7684; Cookie&#xFF08;&#x5982;&#x679C;&#x6709;&#x7684;&#x8BDD;&#xFF09;&#x3002;</p>
<p>&#x5982;&#x679C;&#x9700;&#x8981;&#x8DE8;&#x57DF; AJAX &#x8BF7;&#x6C42;&#x53D1;&#x9001; Cookie&#xFF0C;&#x9700;&#x8981;<code>withCredentials</code>&#x5C5E;&#x6027;&#x8BBE;&#x4E3A;<code>true</code>&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x540C;&#x6E90;&#x7684;&#x8BF7;&#x6C42;&#x4E0D;&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;http://example.com/&apos;</span>, <span class="hljs-literal">true</span>);
xhr.withCredentials = <span class="hljs-literal">true</span>;
xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<p>&#x4E3A;&#x4E86;&#x8BA9;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x751F;&#x6548;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x5FC5;&#x987B;&#x663E;&#x5F0F;&#x8FD4;&#x56DE;<code>Access-Control-Allow-Credentials</code>&#x8FD9;&#x4E2A;&#x5934;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-javascript">Access-Control-Allow-Credentials: <span class="hljs-literal">true</span>
</code></pre>
<p><code>withCredentials</code>&#x5C5E;&#x6027;&#x6253;&#x5F00;&#x7684;&#x8BDD;&#xFF0C;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x4E0D;&#x4EC5;&#x4F1A;&#x53D1;&#x9001; Cookie&#xFF0C;&#x8FD8;&#x4F1A;&#x8BBE;&#x7F6E;&#x8FDC;&#x7A0B;&#x4E3B;&#x673A;&#x6307;&#x5B9A;&#x7684; Cookie&#x3002;&#x53CD;&#x4E4B;&#x4E5F;&#x6210;&#x7ACB;&#xFF0C;&#x5982;&#x679C;<code>withCredentials</code>&#x5C5E;&#x6027;&#x6CA1;&#x6709;&#x6253;&#x5F00;&#xFF0C;&#x90A3;&#x4E48;&#x8DE8;&#x57DF;&#x7684; AJAX &#x8BF7;&#x6C42;&#x5373;&#x4F7F;&#x660E;&#x786E;&#x8981;&#x6C42;&#x6D4F;&#x89C8;&#x5668;&#x8BBE;&#x7F6E; Cookie&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x4E5F;&#x4F1A;&#x5FFD;&#x7565;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x811A;&#x672C;&#x603B;&#x662F;&#x9075;&#x5B88;&#x540C;&#x6E90;&#x653F;&#x7B56;&#xFF0C;&#x65E0;&#x6CD5;&#x4ECE;<code>document.cookie</code>&#x6216;&#x8005; HTTP &#x56DE;&#x5E94;&#x7684;&#x5934;&#x4FE1;&#x606F;&#x4E4B;&#x4E2D;&#xFF0C;&#x8BFB;&#x53D6;&#x8DE8;&#x57DF;&#x7684; Cookie&#xFF0C;<code>withCredentials</code>&#x5C5E;&#x6027;&#x4E0D;&#x5F71;&#x54CD;&#x8FD9;&#x4E00;&#x70B9;&#x3002;</p>
<h3 id="xmlhttprequestupload">XMLHttpRequest.upload</h3>
<p>XMLHttpRequest &#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x6587;&#x4EF6;&#xFF0C;&#x8FD9;&#x5C31;&#x662F; AJAX &#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#x3002;&#x53D1;&#x9001;&#x6587;&#x4EF6;&#x4EE5;&#x540E;&#xFF0C;&#x901A;&#x8FC7;<code>XMLHttpRequest.upload</code>&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x901A;&#x8FC7;&#x89C2;&#x5BDF;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x5F97;&#x77E5;&#x4E0A;&#x4F20;&#x7684;&#x8FDB;&#x5C55;&#x3002;&#x4E3B;&#x8981;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x76D1;&#x542C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5404;&#x79CD;&#x4E8B;&#x4EF6;&#xFF1A;loadstart&#x3001;loadend&#x3001;load&#x3001;abort&#x3001;error&#x3001;progress&#x3001;timeout&#x3002;</p>
<p>&#x5047;&#x5B9A;&#x7F51;&#x9875;&#x4E0A;&#x6709;&#x4E00;&#x4E2A;<code>&lt;progress&gt;</code>&#x5143;&#x7D20;&#x3002;</p>
<pre><code class="lang-http">&lt;progress min=&quot;0&quot; max=&quot;100&quot; value=&quot;0&quot;&gt;0% complete&lt;/progress&gt;
</code></pre>
<p>&#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#x65F6;&#xFF0C;&#x5BF9;<code>upload</code>&#x5C5E;&#x6027;&#x6307;&#x5B9A;<code>progress</code>&#x4E8B;&#x4EF6;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;&#xFF0C;&#x5373;&#x53EF;&#x83B7;&#x5F97;&#x4E0A;&#x4F20;&#x7684;&#x8FDB;&#x5EA6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">upload</span>(<span class="hljs-params">blobOrFile</span>) </span>{
  <span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
  xhr.open(<span class="hljs-string">&apos;POST&apos;</span>, <span class="hljs-string">&apos;/server&apos;</span>, <span class="hljs-literal">true</span>);
  xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{};

  <span class="hljs-keyword">var</span> progressBar = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;progress&apos;</span>);
  xhr.upload.onprogress = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
    <span class="hljs-keyword">if</span> (e.lengthComputable) {
      progressBar.value = (e.loaded / e.total) * <span class="hljs-number">100</span>;
      <span class="hljs-comment">// &#x517C;&#x5BB9;&#x4E0D;&#x652F;&#x6301; &lt;progress&gt; &#x5143;&#x7D20;&#x7684;&#x8001;&#x5F0F;&#x6D4F;&#x89C8;&#x5668;</span>
      progressBar.textContent = progressBar.value;
    }
  };

  xhr.send(blobOrFile);
}

upload(<span class="hljs-keyword">new</span> Blob([<span class="hljs-string">&apos;hello world&apos;</span>], {type: <span class="hljs-string">&apos;text/plain&apos;</span>}));
</code></pre>
<h2 id="xmlhttprequest-&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;">XMLHttpRequest &#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</h2>
<h3 id="xmlhttprequestopen">XMLHttpRequest.open()</h3>
<p><code>XMLHttpRequest.open()</code>&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x6307;&#x5B9A; HTTP &#x8BF7;&#x6C42;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x521D;&#x59CB;&#x5316; XMLHttpRequest &#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x3002;&#x5B83;&#x4E00;&#x5171;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E94;&#x4E2A;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">void</span> open(
   string method,
   string url,
   optional boolean <span class="hljs-keyword">async</span>,
   optional string user,
   optional string password
);
</code></pre>
<ul>
<li><code>method</code>&#xFF1A;&#x8868;&#x793A; HTTP &#x52A8;&#x8BCD;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;<code>GET</code>&#x3001;<code>POST</code>&#x3001;<code>PUT</code>&#x3001;<code>DELETE</code>&#x3001;<code>HEAD</code>&#x7B49;&#x3002;</li>
<li><code>url</code>: &#x8868;&#x793A;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x76EE;&#x6807; URL&#x3002;</li>
<li><code>async</code>: &#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x8BF7;&#x6C42;&#x662F;&#x5426;&#x4E3A;&#x5F02;&#x6B65;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;<code>true</code>&#x3002;&#x5982;&#x679C;&#x8BBE;&#x4E3A;<code>false</code>&#xFF0C;&#x5219;<code>send()</code>&#x65B9;&#x6CD5;&#x53EA;&#x6709;&#x7B49;&#x5230;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x4E86;&#x7ED3;&#x679C;&#xFF0C;&#x624D;&#x4F1A;&#x8FDB;&#x884C;&#x4E0B;&#x4E00;&#x6B65;&#x64CD;&#x4F5C;&#x3002;&#x8BE5;&#x53C2;&#x6570;&#x53EF;&#x9009;&#x3002;&#x7531;&#x4E8E;&#x540C;&#x6B65; AJAX &#x8BF7;&#x6C42;&#x4F1A;&#x9020;&#x6210;&#x6D4F;&#x89C8;&#x5668;&#x5931;&#x53BB;&#x54CD;&#x5E94;&#xFF0C;&#x8BB8;&#x591A;&#x6D4F;&#x89C8;&#x5668;&#x5DF2;&#x7ECF;&#x7981;&#x6B62;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;&#xFF0C;&#x53EA;&#x5141;&#x8BB8; Worker &#x91CC;&#x9762;&#x4F7F;&#x7528;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x8F7B;&#x6613;&#x4E0D;&#x5E94;&#x8BE5;&#x8BBE;&#x4E3A;<code>false</code>&#x3002;</li>
<li><code>user</code>&#xFF1A;&#x8868;&#x793A;&#x7528;&#x4E8E;&#x8BA4;&#x8BC1;&#x7684;&#x7528;&#x6237;&#x540D;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x8BE5;&#x53C2;&#x6570;&#x53EF;&#x9009;&#x3002;</li>
<li><code>password</code>&#xFF1A;&#x8868;&#x793A;&#x7528;&#x4E8E;&#x8BA4;&#x8BC1;&#x7684;&#x5BC6;&#x7801;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x8BE5;&#x53C2;&#x6570;&#x53EF;&#x9009;&#x3002;</li>
</ul>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x5982;&#x679C;&#x5BF9;&#x4F7F;&#x7528;&#x8FC7;<code>open()</code>&#x65B9;&#x6CD5;&#x7684; AJAX &#x8BF7;&#x6C42;&#xFF0C;&#x518D;&#x6B21;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x7B49;&#x540C;&#x4E8E;&#x8C03;&#x7528;<code>abort()</code>&#xFF0C;&#x5373;&#x7EC8;&#x6B62;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x53D1;&#x9001; POST &#x8BF7;&#x6C42;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;POST&apos;</span>, <span class="hljs-built_in">encodeURI</span>(<span class="hljs-string">&apos;someURL&apos;</span>));
</code></pre>
<h3 id="xmlhttprequestsend">XMLHttpRequest.send()</h3>
<p><code>XMLHttpRequest.send()</code>&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5B9E;&#x9645;&#x53D1;&#x51FA; HTTP &#x8BF7;&#x6C42;&#x3002;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5E26;&#x53C2;&#x6570;&#xFF0C;&#x5C31;&#x8868;&#x793A; HTTP &#x8BF7;&#x6C42;&#x53EA;&#x6709;&#x4E00;&#x4E2A; URL&#xFF0C;&#x6CA1;&#x6709;&#x6570;&#x636E;&#x4F53;&#xFF0C;&#x5178;&#x578B;&#x4F8B;&#x5B50;&#x5C31;&#x662F; GET &#x8BF7;&#x6C42;&#xFF1B;&#x5982;&#x679C;&#x5E26;&#x6709;&#x53C2;&#x6570;&#xFF0C;&#x5C31;&#x8868;&#x793A;&#x9664;&#x4E86;&#x5934;&#x4FE1;&#x606F;&#xFF0C;&#x8FD8;&#x5E26;&#x6709;&#x5305;&#x542B;&#x5177;&#x4F53;&#x6570;&#x636E;&#x7684;&#x4FE1;&#x606F;&#x4F53;&#xFF0C;&#x5178;&#x578B;&#x4F8B;&#x5B50;&#x5C31;&#x662F; POST &#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F; GET &#x8BF7;&#x6C42;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>,
  <span class="hljs-string">&apos;http://www.example.com/?id=&apos;</span> + <span class="hljs-built_in">encodeURIComponent</span>(id),
  <span class="hljs-literal">true</span>
);
xhr.send(<span class="hljs-literal">null</span>);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>GET</code>&#x8BF7;&#x6C42;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4F5C;&#x4E3A;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x9644;&#x52A0;&#x5728; URL &#x540E;&#x9762;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x53D1;&#x9001; POST &#x8BF7;&#x6C42;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
<span class="hljs-keyword">var</span> data = <span class="hljs-string">&apos;email=&apos;</span>
  + <span class="hljs-built_in">encodeURIComponent</span>(email)
  + <span class="hljs-string">&apos;&amp;password=&apos;</span>
  + <span class="hljs-built_in">encodeURIComponent</span>(password);

xhr.open(<span class="hljs-string">&apos;POST&apos;</span>, <span class="hljs-string">&apos;http://www.example.com&apos;</span>, <span class="hljs-literal">true</span>);
xhr.setRequestHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>, <span class="hljs-string">&apos;application/x-www-form-urlencoded&apos;</span>);
xhr.send(data);
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x6240;&#x6709; XMLHttpRequest &#x7684;&#x76D1;&#x542C;&#x4E8B;&#x4EF6;&#xFF0C;&#x90FD;&#x5FC5;&#x987B;&#x5728;<code>send()</code>&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x4E4B;&#x524D;&#x8BBE;&#x5B9A;&#x3002;</p>
<p><code>send</code>&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x3002;&#x591A;&#x79CD;&#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">void</span> send();
<span class="hljs-keyword">void</span> send(ArrayBufferView data);
<span class="hljs-keyword">void</span> send(Blob data);
<span class="hljs-keyword">void</span> send(Document data);
<span class="hljs-keyword">void</span> send(<span class="hljs-built_in">String</span> data);
<span class="hljs-keyword">void</span> send(FormData data);
</code></pre>
<p>&#x5982;&#x679C;<code>send()</code>&#x53D1;&#x9001; DOM &#x5BF9;&#x8C61;&#xFF0C;&#x5728;&#x53D1;&#x9001;&#x4E4B;&#x524D;&#xFF0C;&#x6570;&#x636E;&#x4F1A;&#x5148;&#x88AB;&#x4E32;&#x884C;&#x5316;&#x3002;&#x5982;&#x679C;&#x53D1;&#x9001;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#xFF0C;&#x6700;&#x597D;&#x662F;&#x53D1;&#x9001;<code>ArrayBufferView</code>&#x6216;<code>Blob</code>&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x4F7F;&#x5F97;&#x901A;&#x8FC7; Ajax &#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#x6210;&#x4E3A;&#x53EF;&#x80FD;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x53D1;&#x9001;&#x8868;&#x5355;&#x6570;&#x636E;&#x7684;&#x4F8B;&#x5B50;&#x3002;<code>FormData</code>&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x6784;&#x9020;&#x8868;&#x5355;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> formData = <span class="hljs-keyword">new</span> FormData();

formData.append(<span class="hljs-string">&apos;username&apos;</span>, <span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>);
formData.append(<span class="hljs-string">&apos;email&apos;</span>, <span class="hljs-string">&apos;zhangsan@example.com&apos;</span>);
formData.append(<span class="hljs-string">&apos;birthDate&apos;</span>, <span class="hljs-number">1940</span>);

<span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;POST&apos;</span>, <span class="hljs-string">&apos;/register&apos;</span>);
xhr.send(formData);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>FormData</code>&#x5BF9;&#x8C61;&#x6784;&#x9020;&#x4E86;&#x8868;&#x5355;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x4F7F;&#x7528;<code>send()</code>&#x65B9;&#x6CD5;&#x53D1;&#x9001;&#x3002;&#x5B83;&#x7684;&#x6548;&#x679C;&#x4E0E;&#x53D1;&#x9001;&#x4E0B;&#x9762;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&apos;registration&apos;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&apos;registration&apos;</span> <span class="hljs-attr">action</span>=<span class="hljs-string">&apos;/register&apos;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&apos;text&apos;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&apos;username&apos;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&apos;email&apos;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&apos;email&apos;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&apos;zhangsan@example.com&apos;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&apos;number&apos;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&apos;birthDate&apos;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&apos;1940&apos;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&apos;submit&apos;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&apos;return sendForm(this.form);&apos;</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x662F;&#x4F7F;&#x7528;<code>FormData</code>&#x5BF9;&#x8C61;&#x52A0;&#x5DE5;&#x8868;&#x5355;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x53D1;&#x9001;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendForm</span>(<span class="hljs-params">form</span>) </span>{
  <span class="hljs-keyword">var</span> formData = <span class="hljs-keyword">new</span> FormData(form);
  formData.append(<span class="hljs-string">&apos;csrf&apos;</span>, <span class="hljs-string">&apos;e69a18d7db1286040586e6da1950128c&apos;</span>);

  <span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
  xhr.open(<span class="hljs-string">&apos;POST&apos;</span>, form.action, <span class="hljs-literal">true</span>);
  xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// ...</span>
  };
  xhr.send(formData);

  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}

<span class="hljs-keyword">var</span> form = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#registration&apos;</span>);
sendForm(form);
</code></pre>
<h3 id="xmlhttprequestsetrequestheader">XMLHttpRequest.setRequestHeader()</h3>
<p><code>XMLHttpRequest.setRequestHeader()</code>&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x8BBE;&#x7F6E;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x9001;&#x7684; HTTP &#x8BF7;&#x6C42;&#x7684;&#x5934;&#x4FE1;&#x606F;&#x3002;&#x8BE5;&#x65B9;&#x6CD5;&#x5FC5;&#x987B;&#x5728;<code>open()</code>&#x4E4B;&#x540E;&#x3001;<code>send()</code>&#x4E4B;&#x524D;&#x8C03;&#x7528;&#x3002;&#x5982;&#x679C;&#x8BE5;&#x65B9;&#x6CD5;&#x591A;&#x6B21;&#x8C03;&#x7528;&#xFF0C;&#x8BBE;&#x5B9A;&#x540C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x5219;&#x6BCF;&#x4E00;&#x6B21;&#x8C03;&#x7528;&#x7684;&#x503C;&#x4F1A;&#x88AB;&#x5408;&#x5E76;&#x6210;&#x4E00;&#x4E2A;&#x5355;&#x4E00;&#x7684;&#x503C;&#x53D1;&#x9001;&#x3002;</p>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x5934;&#x4FE1;&#x606F;&#x7684;&#x5B57;&#x6BB5;&#x540D;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x6BB5;&#x503C;&#x3002;</p>
<pre><code class="lang-javascript">xhr.setRequestHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>, <span class="hljs-string">&apos;application/json&apos;</span>);
xhr.setRequestHeader(<span class="hljs-string">&apos;Content-Length&apos;</span>, <span class="hljs-built_in">JSON</span>.stringify(data).length);
xhr.send(<span class="hljs-built_in">JSON</span>.stringify(data));
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x9996;&#x5148;&#x8BBE;&#x7F6E;&#x5934;&#x4FE1;&#x606F;<code>Content-Type</code>&#xFF0C;&#x8868;&#x793A;&#x53D1;&#x9001; JSON &#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#xFF1B;&#x7136;&#x540E;&#x8BBE;&#x7F6E;<code>Content-Length</code>&#xFF0C;&#x8868;&#x793A;&#x6570;&#x636E;&#x957F;&#x5EA6;&#xFF1B;&#x6700;&#x540E;&#x53D1;&#x9001; JSON &#x6570;&#x636E;&#x3002;</p>
<h3 id="xmlhttprequestoverridemimetype">XMLHttpRequest.overrideMimeType()</h3>
<p><code>XMLHttpRequest.overrideMimeType()</code>&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x6307;&#x5B9A; MIME &#x7C7B;&#x578B;&#xFF0C;&#x8986;&#x76D6;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x771F;&#x6B63;&#x7684; MIME &#x7C7B;&#x578B;&#xFF0C;&#x4ECE;&#x800C;&#x8BA9;&#x6D4F;&#x89C8;&#x5668;&#x8FDB;&#x884C;&#x4E0D;&#x4E00;&#x6837;&#x7684;&#x5904;&#x7406;&#x3002;&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x662F;<code>text/xml</code>&#xFF0C;&#x7531;&#x4E8E;&#x79CD;&#x79CD;&#x539F;&#x56E0;&#x6D4F;&#x89C8;&#x5668;&#x89E3;&#x6790;&#x4E0D;&#x6210;&#x529F;&#x62A5;&#x9519;&#xFF0C;&#x8FD9;&#x65F6;&#x5C31;&#x62FF;&#x4E0D;&#x5230;&#x6570;&#x636E;&#x4E86;&#x3002;&#x4E3A;&#x4E86;&#x62FF;&#x5230;&#x539F;&#x59CB;&#x6570;&#x636E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A; MIME &#x7C7B;&#x578B;&#x6539;&#x6210;<code>text/plain</code>&#xFF0C;&#x8FD9;&#x6837;&#x6D4F;&#x89C8;&#x5668;&#x5C31;&#x4E0D;&#x4F1A;&#x53BB;&#x81EA;&#x52A8;&#x89E3;&#x6790;&#xFF0C;&#x4ECE;&#x800C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x62FF;&#x5230;&#x539F;&#x59CB;&#x6587;&#x672C;&#x4E86;&#x3002;</p>
<pre><code class="lang-javascript">xhr.overrideMimeType(<span class="hljs-string">&apos;text/plain&apos;</span>)
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5FC5;&#x987B;&#x5728;<code>send()</code>&#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x4FEE;&#x6539;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x4E0D;&#x662F;&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x5E94;&#x8BE5;&#x91C7;&#x53D6;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;<code>responseType</code>&#x5C5E;&#x6027;&#x544A;&#x8BC9;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x3002;&#x53EA;&#x6709;&#x5728;&#x670D;&#x52A1;&#x5668;&#x65E0;&#x6CD5;&#x8FD4;&#x56DE;&#x67D0;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x624D;&#x4F7F;&#x7528;<code>overrideMimeType()</code>&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-keyword">var</span> arraybuffer = xhr.response;
  <span class="hljs-comment">// ...</span>
}
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, url);
xhr.responseType = <span class="hljs-string">&apos;arraybuffer&apos;</span>;
xhr.send();
</code></pre>
<h3 id="xmlhttprequestgetresponseheader">XMLHttpRequest.getResponseHeader()</h3>
<p><code>XMLHttpRequest.getResponseHeader()</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE; HTTP &#x5934;&#x4FE1;&#x606F;&#x6307;&#x5B9A;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x8FD8;&#x6CA1;&#x6709;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x56DE;&#x5E94;&#x6216;&#x8005;&#x6307;&#x5B9A;&#x5B57;&#x6BB5;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE;<code>null</code>&#x3002;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getHeaderTime</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.getResponseHeader(<span class="hljs-string">&quot;Last-Modified&quot;</span>));
}

<span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;HEAD&apos;</span>, <span class="hljs-string">&apos;yourpage.html&apos;</span>);
xhr.onload = getHeaderTime;
xhr.send();
</code></pre>
<p>&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x5B57;&#x6BB5;&#x540C;&#x540D;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x503C;&#x4F1A;&#x88AB;&#x8FDE;&#x63A5;&#x4E3A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6BCF;&#x4E2A;&#x5B57;&#x6BB5;&#x4E4B;&#x95F4;&#x4F7F;&#x7528;&#x201C;&#x9017;&#x53F7;+&#x7A7A;&#x683C;&#x201D;&#x5206;&#x9694;&#x3002;</p>
<h3 id="xmlhttprequestgetallresponseheaders">XMLHttpRequest.getAllResponseHeaders()</h3>
<p><code>XMLHttpRequest.getAllResponseHeaders()</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x6765;&#x7684;&#x6240;&#x6709; HTTP &#x5934;&#x4FE1;&#x606F;&#x3002;&#x683C;&#x5F0F;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6BCF;&#x4E2A;&#x5934;&#x4FE1;&#x606F;&#x4E4B;&#x95F4;&#x4F7F;&#x7528;<code>CRLF</code>&#x5206;&#x9694;&#xFF08;&#x56DE;&#x8F66;+&#x6362;&#x884C;&#xFF09;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x56DE;&#x5E94;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x4E3A;<code>null</code>&#x3002;&#x5982;&#x679C;&#x53D1;&#x751F;&#x7F51;&#x7EDC;&#x9519;&#x8BEF;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;foo.txt&apos;</span>, <span class="hljs-literal">true</span>);
xhr.send();

xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.readyState === <span class="hljs-number">4</span>) {
    <span class="hljs-keyword">var</span> headers = xhr.getAllResponseHeaders();
  }
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6240;&#x6709;&#x5934;&#x4FE1;&#x606F;&#x3002;&#x5B83;&#x53EF;&#x80FD;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code class="lang-http"><span class="hljs-attribute">date</span>: Fri, 08 Dec 2017 21:04:30 GMT\r\n
<span class="hljs-attribute">content-encoding</span>: gzip\r\n
<span class="hljs-attribute">x-content-type-options</span>: nosniff\r\n
<span class="hljs-attribute">server</span>: meinheld/0.6.1\r\n
<span class="hljs-attribute">x-frame-options</span>: DENY\r\n
<span class="hljs-attribute">content-type</span>: text/html; charset=utf-8\r\n
<span class="hljs-attribute">connection</span>: keep-alive\r\n
<span class="hljs-attribute">strict-transport-security</span>: max-age=63072000\r\n
<span class="hljs-attribute">vary</span>: Cookie, Accept-Encoding\r\n
<span class="hljs-attribute">content-length</span>: 6502\r\n
<span class="hljs-attribute">x-xss-protection</span>: 1; mode=block\r\n
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x5BF9;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> arr = headers.trim().split(<span class="hljs-regexp">/[\r\n]+/</span>);
<span class="hljs-keyword">var</span> headerMap = {};

arr.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">line</span>) </span>{
  <span class="hljs-keyword">var</span> parts = line.split(<span class="hljs-string">&apos;: &apos;</span>);
  <span class="hljs-keyword">var</span> header = parts.shift();
  <span class="hljs-keyword">var</span> value = parts.join(<span class="hljs-string">&apos;: &apos;</span>);
  headerMap[header] = value;
});

headerMap[<span class="hljs-string">&apos;content-length&apos;</span>] <span class="hljs-comment">// &quot;6502&quot;</span>
</code></pre>
<h3 id="xmlhttprequestabort">XMLHttpRequest.abort()</h3>
<p><code>XMLHttpRequest.abort()</code>&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x7EC8;&#x6B62;&#x5DF2;&#x7ECF;&#x53D1;&#x51FA;&#x7684; HTTP &#x8BF7;&#x6C42;&#x3002;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4EE5;&#x540E;&#xFF0C;<code>readyState</code>&#x5C5E;&#x6027;&#x53D8;&#x4E3A;<code>4</code>&#xFF0C;<code>status</code>&#x5C5E;&#x6027;&#x53D8;&#x4E3A;<code>0</code>&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;http://www.example.com/page.php&apos;</span>, <span class="hljs-literal">true</span>);
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (xhr) {
    xhr.abort();
    xhr = <span class="hljs-literal">null</span>;
  }
}, <span class="hljs-number">5000</span>);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5728;&#x53D1;&#x51FA;5&#x79D2;&#x4E4B;&#x540E;&#xFF0C;&#x7EC8;&#x6B62;&#x4E00;&#x4E2A; AJAX &#x8BF7;&#x6C42;&#x3002;</p>
<h2 id="xmlhttprequest-&#x5B9E;&#x4F8B;&#x7684;&#x4E8B;&#x4EF6;">XMLHttpRequest &#x5B9E;&#x4F8B;&#x7684;&#x4E8B;&#x4EF6;</h2>
<h3 id="readystatechange-&#x4E8B;&#x4EF6;">readyStateChange &#x4E8B;&#x4EF6;</h3>
<p><code>readyState</code>&#x5C5E;&#x6027;&#x7684;&#x503C;&#x53D1;&#x751F;&#x6539;&#x53D8;&#xFF0C;&#x5C31;&#x4F1A;&#x89E6;&#x53D1; readyStateChange &#x4E8B;&#x4EF6;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>onReadyStateChange</code>&#x5C5E;&#x6027;&#xFF0C;&#x6307;&#x5B9A;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E0D;&#x540C;&#x72B6;&#x6001;&#x8FDB;&#x884C;&#x4E0D;&#x540C;&#x5904;&#x7406;&#x3002;&#x5C24;&#x5176;&#x662F;&#x5F53;&#x72B6;&#x6001;&#x53D8;&#x4E3A;<code>4</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8868;&#x793A;&#x901A;&#x4FE1;&#x6210;&#x529F;&#xFF0C;&#x8FD9;&#x65F6;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5C31;&#x53EF;&#x4EE5;&#x5904;&#x7406;&#x670D;&#x52A1;&#x5668;&#x4F20;&#x9001;&#x56DE;&#x6765;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<h3 id="progress-&#x4E8B;&#x4EF6;">progress &#x4E8B;&#x4EF6;</h3>
<p>&#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#x65F6;&#xFF0C;XMLHttpRequest &#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x672C;&#x8EAB;&#x548C;&#x5B9E;&#x4F8B;&#x7684;<code>upload</code>&#x5C5E;&#x6027;&#xFF0C;&#x90FD;&#x6709;&#x4E00;&#x4E2A;<code>progress</code>&#x4E8B;&#x4EF6;&#xFF0C;&#x4F1A;&#x4E0D;&#x65AD;&#x8FD4;&#x56DE;&#x4E0A;&#x4F20;&#x7684;&#x8FDB;&#x5EA6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateProgress</span> (<span class="hljs-params">oEvent</span>) </span>{
  <span class="hljs-keyword">if</span> (oEvent.lengthComputable) {
    <span class="hljs-keyword">var</span> percentComplete = oEvent.loaded / oEvent.total;
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x65E0;&#x6CD5;&#x8BA1;&#x7B97;&#x8FDB;&#x5C55;&apos;</span>);
  }
}

xhr.addEventListener(<span class="hljs-string">&apos;progress&apos;</span>, updateProgress);

xhr.open();
</code></pre>
<h3 id="load-&#x4E8B;&#x4EF6;&#x3001;error-&#x4E8B;&#x4EF6;&#x3001;abort-&#x4E8B;&#x4EF6;">load &#x4E8B;&#x4EF6;&#x3001;error &#x4E8B;&#x4EF6;&#x3001;abort &#x4E8B;&#x4EF6;</h3>
<p>load &#x4E8B;&#x4EF6;&#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x4F20;&#x6765;&#x7684;&#x6570;&#x636E;&#x63A5;&#x6536;&#x5B8C;&#x6BD5;&#xFF0C;error &#x4E8B;&#x4EF6;&#x8868;&#x793A;&#x8BF7;&#x6C42;&#x51FA;&#x9519;&#xFF0C;abort &#x4E8B;&#x4EF6;&#x8868;&#x793A;&#x8BF7;&#x6C42;&#x88AB;&#x4E2D;&#x65AD;&#xFF08;&#x6BD4;&#x5982;&#x7528;&#x6237;&#x53D6;&#x6D88;&#x8BF7;&#x6C42;&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();

xhr.addEventListener(<span class="hljs-string">&apos;load&apos;</span>, transferComplete);
xhr.addEventListener(<span class="hljs-string">&apos;error&apos;</span>, transferFailed);
xhr.addEventListener(<span class="hljs-string">&apos;abort&apos;</span>, transferCanceled);

xhr.open();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transferComplete</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6570;&#x636E;&#x63A5;&#x6536;&#x5B8C;&#x6BD5;&apos;</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transferFailed</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6570;&#x636E;&#x63A5;&#x6536;&#x51FA;&#x9519;&apos;</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transferCanceled</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7528;&#x6237;&#x53D6;&#x6D88;&#x63A5;&#x6536;&apos;</span>);
}
</code></pre>
<h3 id="loadend-&#x4E8B;&#x4EF6;">loadend &#x4E8B;&#x4EF6;</h3>
<p><code>abort</code>&#x3001;<code>load</code>&#x548C;<code>error</code>&#x8FD9;&#x4E09;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C;&#x4F1A;&#x4F34;&#x968F;&#x4E00;&#x4E2A;<code>loadend</code>&#x4E8B;&#x4EF6;&#xFF0C;&#x8868;&#x793A;&#x8BF7;&#x6C42;&#x7ED3;&#x675F;&#xFF0C;&#x4F46;&#x4E0D;&#x77E5;&#x9053;&#x5176;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;</p>
<pre><code class="lang-javascript">xhr.addEventListener(<span class="hljs-string">&apos;loadend&apos;</span>, loadEnd);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">loadEnd</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8BF7;&#x6C42;&#x7ED3;&#x675F;&#xFF0C;&#x72B6;&#x6001;&#x672A;&#x77E5;&apos;</span>);
}
</code></pre>
<h3 id="timeout-&#x4E8B;&#x4EF6;">timeout &#x4E8B;&#x4EF6;</h3>
<p>&#x670D;&#x52A1;&#x5668;&#x8D85;&#x8FC7;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#x8FD8;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;&#x5C31;&#x4F1A;&#x89E6;&#x53D1; timeout &#x4E8B;&#x4EF6;&#xFF0C;&#x5177;&#x4F53;&#x7684;&#x4F8B;&#x5B50;&#x53C2;&#x89C1;<code>timeout</code>&#x5C5E;&#x6027;&#x4E00;&#x8282;&#x3002;</p>
<h2 id="navigatorsendbeacon">Navigator.sendBeacon()</h2>
<p>&#x7528;&#x6237;&#x5378;&#x8F7D;&#x7F51;&#x9875;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6709;&#x65F6;&#x9700;&#x8981;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#x3002;&#x5F88;&#x81EA;&#x7136;&#x7684;&#x505A;&#x6CD5;&#x662F;&#x5728;<code>unload</code>&#x4E8B;&#x4EF6;&#x6216;<code>beforeunload</code>&#x4E8B;&#x4EF6;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x91CC;&#x9762;&#xFF0C;&#x4F7F;&#x7528;<code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x53D1;&#x9001;&#x6570;&#x636E;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x8FD9;&#x6837;&#x505A;&#x4E0D;&#x662F;&#x5F88;&#x53EF;&#x9760;&#xFF0C;&#x56E0;&#x4E3A;<code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x662F;&#x5F02;&#x6B65;&#x53D1;&#x9001;&#xFF0C;&#x5F88;&#x53EF;&#x80FD;&#x5728;&#x5B83;&#x5373;&#x5C06;&#x53D1;&#x9001;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9875;&#x9762;&#x5DF2;&#x7ECF;&#x5378;&#x8F7D;&#x4E86;&#xFF0C;&#x4ECE;&#x800C;&#x5BFC;&#x81F4;&#x53D1;&#x9001;&#x53D6;&#x6D88;&#x6216;&#x8005;&#x53D1;&#x9001;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x5C31;&#x662F;<code>unload</code>&#x4E8B;&#x4EF6;&#x91CC;&#x9762;&#xFF0C;&#x52A0;&#x4E00;&#x4E9B;&#x5F88;&#x8017;&#x65F6;&#x7684;&#x540C;&#x6B65;&#x64CD;&#x4F5C;&#x3002;&#x8FD9;&#x6837;&#x5C31;&#x80FD;&#x7559;&#x51FA;&#x8DB3;&#x591F;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x4FDD;&#x8BC1;&#x5F02;&#x6B65; AJAX &#x80FD;&#x591F;&#x53D1;&#x9001;&#x6210;&#x529F;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">log</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
  xhr.open(<span class="hljs-string">&apos;post&apos;</span>, <span class="hljs-string">&apos;/log&apos;</span>, <span class="hljs-literal">true</span>);
  xhr.setRequestHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>, <span class="hljs-string">&apos;application/x-www-form-urlencoded&apos;</span>);
  xhr.send(<span class="hljs-string">&apos;foo=bar&apos;</span>);
}

<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;unload&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
  log();

  <span class="hljs-comment">// a time-consuming operation</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">10000</span>; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> m = <span class="hljs-number">1</span>; m &lt; <span class="hljs-number">10000</span>; m++) { <span class="hljs-keyword">continue</span>; }
  }
});
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5F3A;&#x5236;&#x6267;&#x884C;&#x4E86;&#x4E00;&#x6B21;&#x53CC;&#x91CD;&#x5FAA;&#x73AF;&#xFF0C;&#x62D6;&#x957F;&#x4E86;<code>unload</code>&#x4E8B;&#x4EF6;&#x7684;&#x6267;&#x884C;&#x65F6;&#x95F4;&#xFF0C;&#x5BFC;&#x81F4;&#x5F02;&#x6B65; AJAX &#x80FD;&#x591F;&#x53D1;&#x9001;&#x6210;&#x529F;&#x3002;</p>
<p>&#x7C7B;&#x4F3C;&#x7684;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>setTimeout</code>&#x3002;&#x4E0B;&#x9762;&#x662F;&#x8FFD;&#x8E2A;&#x7528;&#x6237;&#x70B9;&#x51FB;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// HTML &#x4EE3;&#x7801;&#x5982;&#x4E0B;</span>
<span class="hljs-comment">// &lt;a id=&quot;target&quot; href=&quot;https://baidu.com&quot;&gt;click&lt;/a&gt;</span>
<span class="hljs-keyword">const</span> clickTime = <span class="hljs-number">350</span>;
<span class="hljs-keyword">const</span> theLink = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;target&apos;</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">log</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
  xhr.open(<span class="hljs-string">&apos;post&apos;</span>, <span class="hljs-string">&apos;/log&apos;</span>, <span class="hljs-literal">true</span>);
  xhr.setRequestHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>, <span class="hljs-string">&apos;application/x-www-form-urlencoded&apos;</span>);
  xhr.send(<span class="hljs-string">&apos;foo=bar&apos;</span>);
}

theLink.addEventListener(<span class="hljs-string">&apos;click&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
  event.preventDefault();
  log();

  setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">window</span>.location.href = theLink.getAttribute(<span class="hljs-string">&apos;href&apos;</span>);
  }, clickTime);
});
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4F7F;&#x7528;<code>setTimeout</code>&#xFF0C;&#x62D6;&#x5EF6;&#x4E86;350&#x6BEB;&#x79D2;&#xFF0C;&#x624D;&#x8BA9;&#x9875;&#x9762;&#x8DF3;&#x8F6C;&#xFF0C;&#x56E0;&#x6B64;&#x4F7F;&#x5F97;&#x5F02;&#x6B65; AJAX &#x6709;&#x65F6;&#x95F4;&#x53D1;&#x51FA;&#x3002;</p>
<p>&#x8FD9;&#x4E9B;&#x505A;&#x6CD5;&#x7684;&#x5171;&#x540C;&#x95EE;&#x9898;&#x662F;&#xFF0C;&#x5378;&#x8F7D;&#x7684;&#x65F6;&#x95F4;&#x88AB;&#x786C;&#x751F;&#x751F;&#x62D6;&#x957F;&#x4E86;&#xFF0C;&#x540E;&#x9762;&#x9875;&#x9762;&#x7684;&#x52A0;&#x8F7D;&#x88AB;&#x63A8;&#x8FDF;&#x4E86;&#xFF0C;&#x7528;&#x6237;&#x4F53;&#x9A8C;&#x4E0D;&#x597D;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x5F15;&#x5165;&#x4E86;<code>Navigator.sendBeacon()</code>&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD8;&#x662F;&#x5F02;&#x6B65;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x4F46;&#x662F;&#x8BF7;&#x6C42;&#x4E0E;&#x5F53;&#x524D;&#x9875;&#x9762;&#x7EBF;&#x7A0B;&#x8131;&#x94A9;&#xFF0C;&#x4F5C;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x8FDB;&#x7A0B;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x4F1A;&#x628A;&#x6570;&#x636E;&#x53D1;&#x51FA;&#x53BB;&#xFF0C;&#x4E0D;&#x62D6;&#x5EF6;&#x5378;&#x8F7D;&#x6D41;&#x7A0B;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;unload&apos;</span>, logData, <span class="hljs-literal">false</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">logData</span>(<span class="hljs-params"></span>) </span>{
  navigator.sendBeacon(<span class="hljs-string">&apos;/log&apos;</span>, analyticsData);
}
</code></pre>
<p><code>Navigator.sendBeacon</code>&#x65B9;&#x6CD5;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x76EE;&#x6807;&#x670D;&#x52A1;&#x5668;&#x7684; URL&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6240;&#x8981;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#xFF08;&#x53EF;&#x9009;&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x662F;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#xFF08;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x8868;&#x5355;&#x5BF9;&#x8C61;&#x3001;&#x4E8C;&#x8FDB;&#x5236;&#x5BF9;&#x8C61;&#x7B49;&#x7B49;&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript">navigator.sendBeacon(url, data)
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x6210;&#x529F;&#x53D1;&#x9001;&#x6570;&#x636E;&#x4E3A;<code>true</code>&#xFF0C;&#x5426;&#x5219;&#x4E3A;<code>false</code>&#x3002;</p>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7684; HTTP &#x65B9;&#x6CD5;&#x662F; POST&#xFF0C;&#x53EF;&#x4EE5;&#x8DE8;&#x57DF;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x8868;&#x5355;&#x63D0;&#x4EA4;&#x6570;&#x636E;&#x3002;&#x5B83;&#x4E0D;&#x80FD;&#x6307;&#x5B9A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// HTML &#x4EE3;&#x7801;&#x5982;&#x4E0B;</span>
<span class="hljs-comment">// &lt;body onload=&quot;analytics(&apos;start&apos;)&quot; onunload=&quot;analytics(&apos;end&apos;)&quot;&gt;</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">analytics</span>(<span class="hljs-params">state</span>) </span>{
  <span class="hljs-keyword">if</span> (!navigator.sendBeacon) <span class="hljs-keyword">return</span>;

  <span class="hljs-keyword">var</span> URL = <span class="hljs-string">&apos;http://example.com/analytics&apos;</span>;
  <span class="hljs-keyword">var</span> data = <span class="hljs-string">&apos;state=&apos;</span> + state + <span class="hljs-string">&apos;&amp;location=&apos;</span> + <span class="hljs-built_in">window</span>.location;
  navigator.sendBeacon(URL, data);
}
</code></pre>




<footer class="page-footer-ex"> <span class="page-footer-ex-copyright"> <a href="https://xiaobaiai.net" target="_blank" rel="nofollow">&#x5C0F;&#x767D;AI.&#x6613;&#x540D;</a>, &#x4F7F;&#x7528;<a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="nofollow">&#x77E5;&#x8BC6;&#x5171;&#x4EAB; 4.0 &#x534F;&#x8BAE;&#x53D1;&#x5E03;</a>&#x53D1;&#x5E03; </span> &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <span class="page-footer-ex-footer-update"> <i>&#x9875;&#x9762;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;</i> 2019-08-22 16:53:02 </span> </footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="cookie.html" class="navigation navigation-prev " aria-label="Previous page: Cookie">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="same-origin.html" class="navigation navigation-next " aria-label="Next page: 同源限制">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"XMLHttpRequest 对象","level":"1.11.5","depth":2,"next":{"title":"同源限制","level":"1.11.6","depth":2,"path":"docs/bom/same-origin.md","ref":"docs/bom/same-origin.md","articles":[]},"previous":{"title":"Cookie","level":"1.11.4","depth":2,"path":"docs/bom/cookie.md","ref":"docs/bom/cookie.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","chapter-fold","back-to-top-button","search-pro","sharing-plus","myseo","favicon","page-footer-ex","search-pro","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"livereload":{},"page-footer-ex":{"copyright":"<a href='https://xiaobaiai.net' target='_blank'>小白AI.易名</a>, 使用<a href='https://creativecommons.org/licenses/by-sa/4.0/' target='_blank'>知识共享 4.0 协议发布</a>发布","markdown":false,"update_format":"YYYY-MM-DD HH:mm:ss","update_label":"<i>页面修订于：</i>"},"search-pro":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"myseo":{"description":"Javascript教程","keywords":"小白AI.易名,xiaobaiai.net,Javascript,入门,教程,博客","titlePrefix":"Javascript教程 - ","titleSuffix":" - 小白AI.易名"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":{"shortcut":"assets/images/favicon.ico","bookmark":"assets/images/favicon.ico","appleTouch":"assets/images/apple-touch-icon.png","appleTouchMore":{"120x120":"assets/images/apple-touch-icon-120x120.png","180x180":"assets/images/apple-touch-icon-180x180.png"}},"back-to-top-button":{},"sharing":{"qq":true,"all":["qq","qzone","twitter","weibo","linkedin"],"douban":false,"facebook":true,"weibo":true,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":true,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小白AI.易名","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*","description":"Javascript教程"},"file":{"path":"docs/bom/xmlhttprequest.md","mtime":"2019-08-22T08:53:02.308Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-08-28T05:00:46.673Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

